import os
import sys
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium.webdriver.common.action_chains import ActionChains
import datetime
class LikeShopTest():
    def __init__(self) -> None:

        driver_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(sys.executable)))),
                               'geckodriver.exe')
        service = FirefoxService(executable_path=driver_path)
        options = webdriver.FirefoxOptions()
        self.browser = webdriver.Firefox(service=service, options=options)
        
    def test_shop_list(self):
        '''
        商品列表测试
        '''
        #点击商品按钮
        self.browser.find_element(By.XPATH,'//*[@id="LAY-system-side-menu"]/li[1]/a/cite').click()

        sleep(2)
        #定位商品子框架
        shop_iframe = self.browser.find_element(By.XPATH,'//*[@id="LAY_app_body"]/div[2]/iframe')
        #进入商品子框架
        self.browser.switch_to.frame(shop_iframe)
        
        #定位商品名称并输入数据
        self.browser.find_element(By.ID,'keyword').send_keys('文具')
        
        #定位查询按钮并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[2]/div/div/div[1]/div/div[5]/button[1]').click()
        sleep(1)
        
        #获取商品列表
        shop_list = self.browser.find_elements(By.XPATH,'/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/table/tbody/tr')
        if len(shop_list) == 0:
            print('没有数据')
        else:
            is_not_bug = True
            for item in shop_list:
                shop_name = item.find_element(By.XPATH,'./td[2]/div').text
                if '文具' not in shop_name:
                    is_not_bug=False
            if is_not_bug:
                print('没有问题')
            else:
                print('有bug')
                
    def test_login(self):
        '''
        登录测试
        '''
        self.browser.get('http://likeshop.abc/admin/account/login.html')
        sleep(2)
        #定位用户名输入框输入用户名
        self.browser.find_element(By.NAME,'account').send_keys('admin')
        
        #定位密码输入框输入密码
        self.browser.find_element(By.NAME,'password').send_keys('123456')
        
        #定位验证码输入框并输入验证码
        self.browser.find_element(By.NAME,'code').send_keys('1111')
        
        #定位登录按钮并点击
        self.browser.find_element(By.ID,'login').click()
        sleep(2)
        
        
    def test_shop_add(self):
        '''
        添加商品
        '''
        # 回到主框架
        self.browser.switch_to.default_content()
        #点击商品按钮
        self.browser.find_element(By.XPATH,'//*[@id="LAY-system-side-menu"]/li[1]/a').click()
        sleep(2)
        #定位子框架
        iframe1 = self.browser.find_element(By.XPATH,'//*[@id="LAY_app_body"]/div[2]/iframe')
        self.browser.switch_to.frame(iframe1)
        
        # 点击发布商品
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[2]/div/div/div[2]/div[1]/button[1]').click()
        sleep(2)
        # 进入商品发布子框架
        shop_add_iframe = self.browser.find_element(By.XPATH,'/html/body/div[3]/div[2]/iframe')
        self.browser.switch_to.frame(shop_add_iframe)
        # 定位商品名称输入框并输入数据
        now = datetime.datetime.now().strftime('%d/%m/%Y, %H:%M:%S')
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[1]/div/input[2]').send_keys('测试商品'+now)
        
        #定位第一个大类的下来框并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/div/input').click()
        sleep(1)
        #定位第一个大类的下拉数据并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/dl/dd[2]').click()
        sleep(1)
        
        # 定位第二个类的下拉框并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/div/input').click()
        sleep(1)
        #定位第二个类的下拉数据并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/dl/dd[2]').click()
        
        # 定位第三个类的下拉框并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/div/input').click()
        sleep(1)
        #定位第三个类的下拉数据并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/dl/dd[2]').click()
        sleep(3)
        
        # 定位商品主图 图片上传按钮
        self.browser.find_element(By.XPATH,'/html/body/div[1]/div/div[1]/div/div/div[5]/div/div[2]/a').click()
        sleep(3)
        #进入图片上传子框架
        upload_img_iframe_1 = self.browser.find_element(By.XPATH,'/html/body/div[3]/div[2]/iframe')
        self.browser.switch_to.frame(upload_img_iframe_1)
        self.test_upload_img('D:\\软件测试\\1.png')
        
        # 回到主框架
        self.browser.switch_to.default_content()
        # 再进入商品子框架
        self.browser.switch_to.frame(iframe1)
        # 再进入商品发布子框架
        self.browser.switch_to.frame(shop_add_iframe)
        
        # 定位商品轮播图 图片上传按钮
        self.browser.find_element(By.XPATH,'/html/body/div[1]/div/div[1]/div/div/div[6]/div/div/a').click()
        #进入图片上传子框架
        sleep(3)
        upload_img_iframe_2 = self.browser.find_element(By.XPATH,'/html/body/div[4]/div[2]/iframe')
        self.browser.switch_to.frame(upload_img_iframe_2)
        self.test_upload_img('D:\\软件测试\\1.png')
        
        # 回到主框架
        self.browser.switch_to.default_content()
        # 再进入商品子框架
        self.browser.switch_to.frame(iframe1)
        # 再进入商品发布子框架
        self.browser.switch_to.frame(shop_add_iframe)
        # 点击价格库存
        self.browser.find_element(By.XPATH,'/html/body/div[1]/ul/li[2]').click()
        sleep(2)
        
        # 定位市场价、价格、成本价、库存输入框并输入数据
        self.browser.find_element(By.XPATH,'//*[@id="one-spec-lists-table"]/tbody/tr/td[2]/input').send_keys('2')
        self.browser.find_element(By.XPATH,'//*[@id="one-spec-lists-table"]/tbody/tr/td[3]/input').send_keys('2')
        self.browser.find_element(By.XPATH,'//*[@id="one-spec-lists-table"]/tbody/tr/td[4]/input').send_keys('1')
        self.browser.find_element(By.XPATH,'//*[@id="one-spec-lists-table"]/tbody/tr/td[5]/input').send_keys('100')
        
        # 回到商品子框架进行保存操作
        # 回到主框架
        self.browser.switch_to.default_content()
        # 再进入商品子框架
        self.browser.switch_to.frame(iframe1)
        self.browser.find_element(By.LINK_TEXT,'保存').click()
        sleep(4)
        
    def test_upload_img(self,file_path):
        # 上传图片
        self.browser.find_element(By.XPATH,"//input[@type='file']").send_keys(file_path)
        sleep(5)
        self.browser.find_element(By.XPATH,'//*[@id="lists"]/li[1]/div/img').click()
        sleep(2)
        self.browser.find_element(By.XPATH,'//*[@id="used"]').click()
        sleep(5)
    
    def test_shop_sj(self):
        '''
        商品上架操作
        '''
        self.browser.switch_to.default_content()
        # 点击商品
        self.browser.find_element(By.XPATH,'//*[@id="LAY-system-side-menu"]/li[1]/a/cite').click()
        sleep(2)
        #定位子框架
        iframe1 = self.browser.find_element(By.XPATH,'//*[@id="LAY_app_body"]/div[2]/iframe')
        self.browser.switch_to.frame(iframe1)
        # 点击仓库中商品
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[2]/ul/li[2]').click()
        sleep(2)
        #获取列表
        shop_list =self.browser.find_elements(By.XPATH,'/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/table/tbody/tr[1]')
        if len(shop_list)!=0:
            # 列表长度不为零则上架
            # 不进行循环，因为上架操作后列表会刷新，固定第一条数据操作
            
            self.browser.find_element(By.XPATH,'/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[3]/div[2]/table/tbody/tr[1]/td/div/a[2]').click()
            sleep(2)
            self.browser.find_element(By.CLASS_NAME,'layui-layer-btn0').click()
            sleep(2)
            
    def test_shop_edit(self):
        '''
        商品编辑
        '''
        #完成部分商品信息的编辑，需要先清楚再输入
        # 回到主框架
        self.browser.switch_to.default_content()
        # 点击商品按钮
        self.browser.find_element(By.XPATH,'//*[@id="LAY-system-side-menu"]/li[1]/a/cite').click()
        sleep(2)
        # 定位子框架
        iframe1 = self.browser.find_element(By.XPATH,'//*[@id="LAY_app_body"]/div[2]/iframe')
        # 进入商品子框架
        self.browser.switch_to.frame(iframe1)
        
        # 点击编辑按钮
        self.browser.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/div/div/div[2]/div[2]/div[1]/div[3]/div[2]/table/tbody/tr[1]/td/div/a[1]').click()
        sleep(2)
        # 进入商品编辑子框架
        shop_edit_iframe = self.browser.find_element(By.XPATH,'/html/body/div[3]/div[2]/iframe')
        self.browser.switch_to.frame(shop_edit_iframe)
        # 清除商品的名称
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[1]/div/input[2]').clear()
        sleep(2)
        # 输入修改商品名称
        now = datetime.datetime.now().strftime('%d/%m/%Y, %H:%M:%S')
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[1]/div/input[2]').send_keys('修改商品'+now)
        sleep(2)
        
        # 回到主框架
        self.browser.switch_to.default_content()
        # 再进入商品子框架
        self.browser.switch_to.frame(iframe1)
        # 再进入编辑商品子框架
        self.browser.switch_to.frame(shop_edit_iframe)

        # 点击价格库存
        self.browser.find_element(By.XPATH,'/html/body/div[1]/ul/li[2]').click()
        sleep(2)
        #清除商品的市场价
        self.browser.find_element(By.XPATH,'//*[@id="one-spec-lists-table"]/tbody/tr/td[2]/input').clear()
        sleep(2)
        #输入修改的商品市场价
        self.browser.find_element(By.XPATH,'//*[@id="one-spec-lists-table"]/tbody/tr/td[2]/input').send_keys('5')
        sleep(2)
        
        # 回到主框架
        self.browser.switch_to.default_content()
        # 再进入商品子框架
        self.browser.switch_to.frame(iframe1)
        # 再进入编辑商品子框架
        self.browser.switch_to.frame(shop_edit_iframe)
        
        
    def test_logout(self):
        self.browser.switch_to.default_content()
        #鼠标悬停
        dt = self.browser.find_element(By.XPATH,'//*[@id="LAY_app"]/div[1]/div[1]/ul[2]/li[3]/a/cite')
        ActionChains(self.browser).move_to_element(dt).perform()
        sleep(2)
        self.browser.find_element(By.XPATH,'//*[@id="LAY_app"]/div[1]/div[1]/ul[2]/li[3]/dl/dd[2]/a').click()
        
        
        
        
if __name__ == '__main__':
    likeShopTest = LikeShopTest()
    likeShopTest.test_login()
    likeShopTest.test_shop_list()
    likeShopTest.test_shop_add()
    likeShopTest.test_shop_sj()
    likeShopTest.test_shop_edit()
    likeShopTest.test_logout()